package com.yangyu.service;

import com.yangyu.mapper.*;
import com.yangyu.message.BaseRsp;
import com.yangyu.message.Page;
import com.yangyu.message.blog.BlogInfo;
import com.yangyu.message.blog.EditBlogReq;
import com.yangyu.message.blog.QueryBlogReq;
import com.yangyu.message.blog.SimpleBlogVo;
import com.yangyu.pojo.Blog;
import com.yangyu.pojo.BlogTag;
import com.yangyu.pojo.Tag;
import com.yangyu.util.SnowflakeIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class BlogService {

    @Autowired
    private BlogMapper mapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private BlogTagMapper blogTagMapper;

    /**
     * 批量查询博客
     *
     * @param req 查询条件
     * @return 博客
     */
    public BaseRsp getBlogs(QueryBlogReq req) {
        int start = (req.getCurrentPage() - 1) * req.getPageSize();
        req.setStart(start);
        List<BlogInfo> blogs;
        int total;
        try {
            blogs = mapper.getBlogs(req);
            total = mapper.countBlogs(req);
            blogs.forEach(blogInfo -> {
                // blogInfo.setTags(tagMapper.getFlagsByBlogId(Long.valueOf(blogInfo.getId())));
                blogInfo.setCommentNum(commentMapper.countComments(blogInfo.getId()));
            });
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseRsp(630100, "dbError | get blogs failed");
        }

        return new BaseRsp(blogs, new Page(req.getCurrentPage(), req.getPageSize(), total));
    }

    /**
     * 根据id查询博客
     *
     * @param blog 博客
     * @return 博客
     */
    public BaseRsp getBlogById(Blog blog) {
        if (blog == null) {
            return new BaseRsp(630002, "blog is empty");
        }
        long blogId = blog.getId();
        BlogInfo blogInfo;
        try {
            blogInfo = mapper.getBlogById(blogId);
            blogInfo.setTags(tagMapper.getFlagsByBlogId(blogId));
            blogInfo.setCommentNum(commentMapper.countComments(blogId));
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseRsp(630100, "dbError | get blogById failed");
        }
        return new BaseRsp(blogInfo);
    }

    /**
     * 编辑博客
     *
     * @param blog 博客
     * @return 成功数
     */
    public BaseRsp editBlog(EditBlogReq blog) {
        String userName = userMapper.getUserNameById(blog.getUserId());
        List<Tag> tags = blog.getTags();
        blog.setUserName(userName);

        long id = SnowflakeIdUtil.generateId();
        try {
            if (blog.getId() != 0) {
                mapper.editBlog(blog);
                blogTagMapper.deleteBlogTagsByBlogId(blog.getId());
                addBlogTags(blog.getId(), tags);
                return new BaseRsp(String.valueOf(blog.getId()));
            } else {
                blog.setId(id);
                mapper.addBlog(blog);
                addBlogTags(id, tags);
                return new BaseRsp(String.valueOf(id));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseRsp(630100, "dbError | edit blog failed");
        }
    }

    private void addBlogTags(long id, List<Tag> tags) {
        List<BlogTag> blogTags = new ArrayList<>();
        tags.forEach(tag -> blogTags.add(new BlogTag(id, tag.getId())));
        blogTagMapper.addBlogTags(blogTags);
    }

    public BaseRsp addBlog(Blog blog) {
        if (blog == null) {
            return new BaseRsp(630002, "blog is empty");
        }
        try {
            mapper.addBlog(blog);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseRsp(630100, "dbError | add blog failed");
        }
        return new BaseRsp();
    }

    /**
     * 删除博客
     *
     * @param blog 博客
     * @return 成功数
     */
    public BaseRsp deleteBlog(Blog blog) {
        if (blog == null) {
            return new BaseRsp(630002, "blog is empty");
        }
        try {
            mapper.deleteBlog(blog.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseRsp(630100, "dbError | delete blog failed");
        }
        return new BaseRsp();
    }

    /**
     * 获取收藏的博客
     *
     * @param req 请求
     * @return 结果
     */
    public BaseRsp getLikeBlogs(QueryBlogReq req) {
        int start = (req.getCurrentPage() - 1) * req.getPageSize();
        req.setStart(start);

        List<BlogInfo> blogs;
        int total;
        try {
            blogs = mapper.getLikeBlogs(req);
            total = mapper.countLikeBlogs(req);
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseRsp(630100, "dbError | get blogs failed");
        }

        return new BaseRsp(blogs, new Page(req.getCurrentPage(), req.getPageSize(), total));
    }

    /**
     * 查询热门博客
     *
     * @return
     */
    public BaseRsp getHotBlogs() {
        List<SimpleBlogVo> hotBlog = mapper.getHotBlog();

        hotBlog.forEach(blog -> {
            blog.setCommentNum(commentMapper.countComments(blog.getId()));
        });
        Collections.sort(hotBlog);
        return new BaseRsp(hotBlog);
    }

    /**
     * 查询最新博客
     *
     * @return
     */
    public BaseRsp getNewBlogs() {
        List<SimpleBlogVo> hotBlog = mapper.getNewBlog();
        return new BaseRsp(hotBlog);
    }
}
